package template;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

class Pet{
    String sName;
    public Pet(String sName){
        this.sName = sName;
    }
}
class Cat extends Pet{
    public Cat(){
        super("cat");
    }
}
class Dog extends Pet{
    public Dog(){
        super("dog");
    }
}
public class CheckedList {
    static void oldStyleMethod(List probablyDogs){
        probablyDogs.add(new Cat());
    }
    public static void main(String[] args){
        List<Dog> dogs1 = new ArrayList<Dog>();
        oldStyleMethod(dogs1);
        List<Dog> dogs2 = Collections.checkedList(new ArrayList<Dog>(),Dog.class);
        try{
            oldStyleMethod(dogs2);
        }catch (Exception e){
            System.out.println(e);
        }
        List<Pet> pets = Collections.checkedList(new ArrayList<Pet>(),Pet.class);
        pets.add(new Dog());
        pets.add(new Cat());
    }
}
